package com.chenlipeng.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import net.sf.json.JSONArray;


/**
* @parameter 
* @author    ChenLiPeng
* @date      2017年10月10日
 */
public class JdzXyLogin {
	
	private static String ASPSESSIONIDQADQQSAQ;
	
	public static List<SocreResultBean> isLogin(Map<String, Object> map) {
		
		// 第二种写法
		DefaultHttpClient hc=new DefaultHttpClient();
//		CloseableHttpClient hc = HttpClientUtils.getHttpClient();
		
		HttpGet hp = new HttpGet("http://218.65.59.52/jwxs/LoginCheck.asp?LoginLb=xs&Account=140305010315&PassWord=12345678a");  // 登陆
		HttpGet hg = new HttpGet("http://218.65.59.52/jwxs/Xsxk/default.asp");
		HttpResponse responsePost = null;
		HttpResponse responsePost1 = null;
		try {
			hp.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			hp.addHeader("Accept-Encoding", "gzip, deflate");
			hp.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
			hp.addHeader("Cache-Control", "max-age=0");
			hp.addHeader("Referer", "http://218.65.59.52/jwxs/login2.asp?LoginLb=xs&Datetime=2017-10-13%2023:27:37");
			hp.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36");
			responsePost = hc.execute(hp);
			Document doc1 = Jsoup.parse(EntityUtils.toString(responsePost.getEntity(), "GBK"));
			System.out.println(doc1.toString());
			
			String cookie1="";
			 Header[] headers1 = responsePost.getAllHeaders();
             for (Header header : headers1) {
            	 if (header.getName().equals("Set-Cookie") ) { 
            		 if (header.getValue().indexOf("ASPSESSIONIDQADQQSAQ")!=-1 || header.getValue().indexOf("ASPSESSIONIDSCAQRSBR")!=-1) {
            			 cookie1= header.getValue().substring(header.getValue().indexOf("=")+1, header.getValue().indexOf(";"));
            			 ASPSESSIONIDQADQQSAQ=cookie1;
					}
				}
             }
			CloseableHttpClient hc1 = HttpClients.custom().setDefaultCookieStore(hc.getCookieStore()).build();
			hg.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			hg.addHeader("Accept-Encoding", "gzip, deflate");
			hg.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
			hg.addHeader("Cache-Control", "max-age=0");
			hg.addHeader("Connection", "keep-alive");
			hg.addHeader("Host", "218.65.59.52");
			hg.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36");
			hg.addHeader(new BasicHeader("Cookie","ASPSESSIONIDQADQQSAQ="+cookie1+"; OpenWindow=; CurrQzjiaowuLoginLb=xs;"));
			responsePost1=hc1.execute(hg);
			Document doc3 = Jsoup.parse(EntityUtils.toString(responsePost1.getEntity(), "GBK"));
			System.out.println(doc3.toString());
			List<SocreResultBean> scoreList2=getScore();
			return scoreList2;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(hp != null){
				hp.abort();
			}
		}
		return null;
	}
	
	
	private static List<SocreResultBean> getScore() {
		
		ArrayList<SocreResultBean> scoreList2=new ArrayList<SocreResultBean>();
		try {
			String url="http://218.65.59.52/jwxs/Xsxk/Xk_CjZblist.asp?flag=find";
			Map<String, String> cookie=new HashMap<String, String>();
			cookie.put("ASPSESSIONIDSCAQRSBR", ASPSESSIONIDQADQQSAQ);
			cookie.put("LoginLb", "xs");
			cookie.put("CurrQzjiaowuLoginLb", "xs");
			cookie.put("OpenWindow", "");
			Element doc2 = Jsoup.connect(url).cookies(cookie).get();
			Element table = doc2.getElementById("mxh");
			table.getElementsByTag("tr").get(0).remove();  // 移除标题的tr
			int trCount=table.getElementsByTag("tr").toArray().length;
			for (int i = 0; i < trCount; i++) {
				Element content3 = table.getElementById("tr1_"+(i+1));
				Elements count = content3.getElementsByTag("td");  // 如果count=17,说明是2013-2014学年.如果count=15,说明是201301
				 SocreResultBean socre=new SocreResultBean();
				 socre.setYears(count.get(1).text());
				 socre.setCourse(count.get(2).text());
				 socre.setCredit(count.get(3).text());
				 socre.setScore(count.get(4).text());
				 socre.setState(count.get(8).text());
				 scoreList2.add(socre);
			}
			System.out.println(doc2.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return scoreList2;
		
	}
}
